package com.example.hnvehicle.controller;

import com.example.hnvehicle.service.impl.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class SecurityController {
    @Autowired
    private MyUserDetailsService userDetailsService;

    @GetMapping("/permissions")
    public String getUserPermissions(@AuthenticationPrincipal UserDetails userDetails) {
        // 从当前登录用户的 Principal 对象中获取用户名
        String username = userDetails.getUsername();
        String power="";
        // 根据用户名调用 MyUserDetailsService 类中的 loadUserByUsername 方法
        UserDetails user = userDetailsService.loadUserByUsername(username);


        // 返回用户角色列表
        if(user.getAuthorities().equals("[ROLE_sale]")){
            power="普通用户";
        }else {
            power="管理员";
        }
        return user.getUsername()+" "+power;
    }
}
